﻿using ApplicationModels.A07.Chats;
using Commons.Helpers;
using Commons.Requests;
using Commons.Responses;
using EntityModels.E07.Chats;
using IApplicationServices;
using Microsoft.AspNetCore.Mvc;

namespace WebAPI.Controllers
{
    [Route("[controller]/[action]")]
    public class MessageController:BaseController<int,Message,MessageAM>
    {
        public MessageController(IMessageService service) : base(service)
        {

        }
        public override async Task<DatasResponse<MessageAM>> GetBySelectAsync([FromQuery] PagingRequest pag)
        {
            return await ApiService.GetBySelectAsync(pag.PageIndex,pag.PageSize,x=>x.ConversationId == Int32.Parse(pag.SearchValue));
        }

        [HttpGet]
        public async Task<DatasResponse<MessageAM>> GetByConversationIdAsync(int conversationId)
        {
            var conver = await ApiService.GetOtherAsync<int, Conversation, ConversationAM>(x => x.Id == conversationId);
            if (conver.Successed)
            {
                return await ApiService.GetAllAsync(x => x.ConversationId == conversationId);
            }
            else
            {
                throw new CustomException("当前会话不存在");
            }
        }
    }
}
